//
//  DSPTableViewCell.swift
//  SWweibo
//
//  Created by 杜守鹏 on 16/1/4.
//  Copyright © 2016年 杜守鹏. All rights reserved.
//

import UIKit
import SnapKit
// cell 里面的间距
let DSPStatusCellMargin: CGFloat = 8
// 头像的宽高
let DSPStatusCellIconViewWH: CGFloat = 35

class DSPTableViewCell: UITableViewCell {
    
    // toolBar 的顶部约束
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: DSPStatusViewModel?{
        didSet{
            statusOriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
             self.toolBarTopConstraint?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                statusRetweetView.hidden = false
                statusRetweetView.statusViewModel = statusViewModel
                
                statusToolBar.snp_updateConstraints { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                }

            }else{
                statusRetweetView.hidden = true
                statusToolBar.snp_updateConstraints { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                }

            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func  setUpUI(){
        
        // 添加视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusRetweetView)
        contentView.addSubview(statusToolBar)
        
        // 添加约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(statusOriginalView)
            make.trailing.equalTo(statusOriginalView)
            make.top.equalTo(statusOriginalView.snp_bottom)
        }
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
              self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        // 非常重要
        contentView.snp_makeConstraints { (make) -> Void in
            // 决定 cell 的高度
            make.bottom.equalTo(statusToolBar.snp_bottom)
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    // MARK: - 懒加载控件
    private lazy var statusOriginalView: DSPStatusOriginalView = DSPStatusOriginalView()
    private lazy var statusToolBar: DSPStatusToolBar = DSPStatusToolBar()
    // 转发微博
    private lazy var statusRetweetView: DSPStatusRetweetView = DSPStatusRetweetView()
    
}
